<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加离线明细' : '编辑离线明细'" :visible.sync="addEditIsDialog_c" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="24">
            <el-form-item label="提醒类型：" prop="type">
              <el-select v-model="ruleForm.type" placeholder="请选择提醒类型" clearable :disabled="seeType">
                <el-option label="当日" :value="0"></el-option>
                <el-option label="次日" :value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="推送方式：" prop="pushMode"><el-select multiple v-model="ruleForm.pushMode" placeholder="请选择推送方式" clearable>
                <el-option label="邮件" value="0"></el-option>
                <el-option label="短信" value="1"></el-option>
                <el-option label="语音" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="ruleForm.type == 0">
          <el-col :span="24">
            <el-form-item label="间隔分钟：" prop="intervalTime">
              <el-input-number v-model="ruleForm.intervalTime" placeholder="请输入间隔分钟" :precision="0" :step="1" :min="1" :max="1440" :disabled="seeType"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="ruleForm.type == 1">
          <el-col :span="24">
            <el-form-item label="间隔天数：" prop="intervalDay">
              <el-input-number v-model="ruleForm.intervalDay" placeholder="请输入间隔天数" :precision="0" :step="1" :min="1" :max="14" :disabled="seeType"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="ruleForm.type == 1">
          <el-col :span="24">
            <el-form-item label="提醒时间：" prop="remindTime">
              <el-time-select v-model="ruleForm.remindTime" :picker-options="{
                  start: '00:00',
                  step: '00:05',
                  end: '23:59',
                }" placeholder="请选择提醒时间" :disabled="seeType" style="width: 100%">
              </el-time-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getModelOfflineDetail, saveModelAlarmDetail } from '@/api/offline'

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: ''
    },
    addEditStatus: {
      type: String,
      default: 'add'
    },
    addEditIsDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: '',
        modelOfflineSettingsId: '',
        type: '',
        intervalTime: '',
        intervalDay: '',
        remindTime: ''
      },
      rules: {
        // 表单规则
        type: [{ required: true, message: '请选择提醒类型', trigger: 'blur' }],
        intervalTime: [{ required: true, message: '请输入间隔分钟', trigger: 'blur' }],
        intervalDay: [{ required: true, message: '请输入间隔天数', trigger: 'blur' }],
        remindTime: [{ required: true, message: '请选择离提醒时间', trigger: 'change' }]
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false // 是否禁用
    }
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == 'add') {
          this.ruleForm.id = ''
          this.seeType = false
        }

        // 编辑
        if (this.addEditStatus == 'edit') {
          this.ruleForm.id = this.addEditId
          this.seeType = false
          this.getDetail()
        }

        // 查看
        if (this.addEditStatus == 'see') {
          this.ruleForm.id = this.addEditId
          this.seeType = true
          this.getDetail()
        }

        this.ruleForm.modelOfflineSettingsId = this.$route.query.modelOfflineSettingsId
      }
    }
  },
  methods: {
    // 详情
    getDetail() {
      getModelOfflineDetail({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          if (res.data.pushMode) {
            res.data.pushMode = res.data.pushMode.split(',')
          }
          if (res.data.intervalTime == null) {
            res.data.intervalTime = undefined
          }
          if (res.data.intervalDay == null) {
            res.data.intervalDay = undefined
          }

          this.ruleForm = res.data
        }
      })
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.ruleForm.type == 0) {
            this.ruleForm.intervalDay = ''
            this.ruleForm.remindTime = ''
          } else {
            this.ruleForm.intervalTime = ''
          }
          this.ruleForm.pushMode = this.ruleForm.pushMode.join(',')
          saveModelAlarmDetail(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })

              // 关闭弹窗
              this.$parent.closeAddEdit()
              // 刷新列表
              this.$parent.onSubmit()
            }
          })
        }
      })
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.$parent.closeAddEdit()
    }
  }
}
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }
}
</style>
